//=======================================================================================
//
// Copyright (C) 2010-2011 Asia-Peak Technologies, Inc. All Rights Reserved.
// 
// All the code, text, graphics, media, design, programs and other works are
// protected by copyright law. Unauthorized reproduction or redistribution of them, 
// or any portion of them, are forbidden.
// 
//=======================================================================================

using System;
using System.Collections.Generic;

namespace CSP.Common.Web
{
    public class LogManager
    {
        private static object obj = new object();
        private static LogManager m_instance = null;

        public static LogManager Instance
        {
            get
            {
                if (m_instance == null)
                {
                    lock (obj)
                    {
                        if (m_instance == null)
                        {
                            m_instance = new LogManager();
                        }
                    }
                }
                return m_instance;
            }
        }
        protected Dictionary<string, log4net.ILog> LogDict = new Dictionary<string, log4net.ILog>();
        protected LogManager()
        {
            log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(System.AppDomain.CurrentDomain.BaseDirectory + "Log\\Log4net.config"));//读取配置文件
            this.CommonLog = log4net.LogManager.GetLogger("CommonLog");
            this.LogDict["CommonLog"] = this.CommonLog;
            this.ErrorLog = log4net.LogManager.GetLogger("ErrorLog");
            this.LogDict["ErrorLog"] = this.ErrorLog;
        }

        /// <summary>
        /// 通用日志记录器
        /// </summary>
        public log4net.ILog CommonLog
        {
            get;
            private set;
        }

        /// <summary>
        /// 错误日志记录器
        /// </summary>
        public log4net.ILog ErrorLog
        {
            get;
            private set;
        }

        /// <summary>
        /// 获取指定名称的日志记录器。
        /// </summary>
        /// <param name="name">日志记录器名称</param>
        /// <returns></returns>
        public log4net.ILog this[string name]
        {
            get
            {
                if (this.LogDict.ContainsKey(name))
                    return this.LogDict[name];
                return this.LogDict[name] = log4net.LogManager.GetLogger(name);
            }
        }
    }
}
